package com.cwt_taxi_xa.db.dal;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.cwt_taxi_xa.model.QuestionInfo;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.ExceptionUtil;

import java.util.ArrayList;

public class QuestionDao {
	private ContentValues values;
	private DBOpenHelper helper;
	private static final String TAG = "EventDao";

	public QuestionDao(Context context){
		values = new ContentValues();
		helper = new DBOpenHelper(context);
	}

	public synchronized int updateReadStatus(int id){
		int rowId = 0;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			rowId = 0;
			values.clear();
			values.put(DBConstants.QUESTION_IS_READ, 1);
			rowId = db.update(DBConstants.TABLE_QUESTION, values,
					DBConstants.QUESTION_ID+"=?", new String[]{""+id});
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rowId;
	}

	public synchronized int updateDownStatus(QuestionInfo info){
		int rowId = 0;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			rowId = 0;
			values.clear();
			values.put(DBConstants.QUESTION_IS_DONE, 1);
			values.put(DBConstants.QUESTION_ANSWER_ID, info.getAnswerId());
			rowId = db.update(DBConstants.TABLE_QUESTION, values,
					DBConstants.QUESTION_ID+"=?", new String[]{""+info.getId()});
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rowId;
	}

	/**
	 * 获取总条数
	 * @author ferch
	 * @return
	 */
	public int getCount() {
		// TODO Auto-generated method stub
		try {
			SQLiteDatabase db = helper.getReadableDatabase();
			String sql = "select count(*) from "+DBConstants.TABLE_QUESTION;
			Cursor c = db.rawQuery(sql, null);
			c.moveToFirst();
			int length = c.getInt(0);
			db.close();
			return length;
		} catch (Exception e) {
			AppLog.e(ExceptionUtil.getInfo(e), e);
			e.printStackTrace();
			// TODO: handle exception
			return 0;
		}
	}

	public synchronized ArrayList<QuestionInfo> getQuestions(int currentPage, int pageCount) {
		ArrayList<QuestionInfo> infos = null;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			db.beginTransaction();
			String sql = "1 = 1 order by "+DBConstants.QUESTION_CREATE_TIME+
					" desc limit "+((currentPage-1)*pageCount)+","+pageCount;
			// 执行查询
			Cursor c = db.query(DBConstants.TABLE_QUESTION,
					null, sql, null, null, null, null);
			// 遍历结果
			if (c != null) {
				infos = new ArrayList<QuestionInfo>();
				while (c.moveToNext()) {
					QuestionInfo info = new QuestionInfo();
					info.setId(c.getInt(c.getColumnIndex(DBConstants.QUESTION_ID)));
					info.setIsRead(c.getInt(c.getColumnIndex(DBConstants.QUESTION_IS_READ)));
					info.setIsDone(c.getInt(c.getColumnIndex(DBConstants.QUESTION_IS_DONE)));
					info.setAnswerId(c.getInt(c.getColumnIndex(DBConstants.QUESTION_ANSWER_ID)));
					info.setCreatTime(c.getString(c
							.getColumnIndex(DBConstants.QUESTION_CREATE_TIME)));
					info.setFlag(c.getString(c
							.getColumnIndex(DBConstants.QUESTION_FLAG)));
					info.setText(c.getString(c
							.getColumnIndex(DBConstants.QUESTION_TEXT)));
					info.setAnswer(c.getString(c
							.getColumnIndex(DBConstants.QUESTION_ANSWER)));
					info.setSerialId(c.getInt(c
							.getColumnIndex(DBConstants.QUESTION_SERIAL_ID)));
					infos.add(info);
				}
			}
			//设置事务标志为成功，当结束事务时就会提交事务
			db.setTransactionSuccessful();
			db.endTransaction();
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return infos;
	}

	public synchronized int save(QuestionInfo info) {
		// TODO Auto-generated method stub
		int count = 0;
		long rowId = 0;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			values.clear();
			values.put(DBConstants.QUESTION_ID, info.getId());
			values.put(DBConstants.QUESTION_SERIAL_ID, info.getSerialId());
			values.put(DBConstants.QUESTION_CREATE_TIME, info.getCreatTime());
			values.put(DBConstants.QUESTION_IS_READ, info.getIsRead());
			values.put(DBConstants.QUESTION_TEXT, info.getText());
			values.put(DBConstants.QUESTION_FLAG, info.getFlag());
			values.put(DBConstants.QUESTION_IS_DONE, info.getIsDone());
			values.put(DBConstants.QUESTION_ANSWER, info.getAnswer());

			/*	rowId = db.insert(DBConstants.TABLE_QUESTION, null, values);
			if(rowId != 0){
				AppLog.i(TAG, "添加提问，rowId："+rowId);
			}*/

			// 执行查询
			Cursor c = db.query(DBConstants.TABLE_QUESTION,
					null, DBConstants.QUESTION_ID+"=?", new String[]{""+info.getId()}, null, null, null);
			if(c != null && c.moveToFirst()){
				rowId = db.update(DBConstants.TABLE_QUESTION, values,
						DBConstants.QUESTION_ID+"=?", new String[]{""+info.getId()});
				if(rowId != 0){
					AppLog.i(TAG, "更新提问，rowId："+rowId);
				}
			}else{
				rowId = db.insert(DBConstants.TABLE_QUESTION, null, values);
				if(rowId != 0){
					AppLog.i(TAG, "添加提问，rowId："+rowId);
				}
			}
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return count;
		}
		return count;
	}

	public synchronized int save(ArrayList<QuestionInfo> infos) {
		// TODO Auto-generated method stub
		int count = 0;
		long rowId = 0;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			db.beginTransaction();
			for(int i = 0;i<infos.size();i++){
				QuestionInfo info = infos.get(i);
				values.clear();
				values.put(DBConstants.QUESTION_ID, info.getId());
				values.put(DBConstants.QUESTION_CREATE_TIME, info.getCreatTime());
				values.put(DBConstants.QUESTION_IS_READ, info.getIsRead());
				values.put(DBConstants.QUESTION_TEXT, info.getText());
				values.put(DBConstants.QUESTION_IS_DONE, info.getIsDone());
				values.put(DBConstants.QUESTION_ANSWER, info.getAnswer());
				values.put(DBConstants.QUESTION_FLAG, info.getFlag());
				values.put(DBConstants.QUESTION_SERIAL_ID, info.getSerialId());

				rowId = db.insert(DBConstants.TABLE_QUESTION, null, values);
				if(rowId != 0){
					AppLog.i(TAG, "添加提问，rowId："+rowId);
				}
			}
			//设置事务标志为成功，当结束事务时就会提交事务
			db.setTransactionSuccessful();
			db.endTransaction();
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return count;
		}
		return count;
	}

	/*public synchronized int update(ArrayList<QuestionInfo> infos) {
		// TODO Auto-generated method stub
		int count = 0;
		long rowId = 0;
		try {
			SQLiteDatabase db = helper.getWritableDatabase();
			db.beginTransaction();
			for(int i = 0;i<infos.size();i++){
				QuestionInfo info = infos.get(i);
				values.clear();
				values.put(DBConstants.QUESTION_ID, info.getId());
				values.put(DBConstants.QUESTION_CREATE_TIME, info.getCreatTime());
				values.put(DBConstants.QUESTION_IS_READ, info.getIsRead());
				values.put(DBConstants.QUESTION_TEXT, info.getText());
				values.put(DBConstants.QUESTION_IS_DONE, info.getIsDone());
				values.put(DBConstants.QUESTION_ANSWER, info.getAnswer());

				rowId = db.update(DBConstants.TABLE_QUESTION, values,
						DBConstants.QUESTION_ID+"=?", new String[]{""+info.getId()});
				if(rowId != 0){
					AppLog.i(TAG, "更新提问，rowId："+rowId);
				}
			}
			//设置事务标志为成功，当结束事务时就会提交事务
			db.setTransactionSuccessful();
			db.endTransaction();
			db.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return count;
		}
		return count;
	}*/

	public synchronized int clear() {
		// TODO Auto-generated method stub
		int result = -1;
		SQLiteDatabase db = helper.getWritableDatabase();
		result = db.delete(DBConstants.TABLE_QUESTION, null, null);
		//		db.execSQL("delete from sqlite_sequence where name = \"" + Constants.TABLENAME+ "\"");
		db.execSQL("update sqlite_sequence set seq = 0 where name = \""+ DBConstants.TABLE_QUESTION+ "\"");
		AppLog.i(TAG, "清空表数据: tableName"+DBConstants.TABLE_QUESTION+"; result:"+result);
		db.close();
		return result;
	}

	/**
	 * 删除事件
	 * @return
	 */
	/*public int delete(ArrayList<QuestionInfo> infos){
		int delCount = 0;
		SQLiteDatabase db = helper.getWritableDatabase();
		db.beginTransaction();
		for(int i = 0;i<infos.size();i++){
			int id = infos.get(i).getId();
			int result = db.delete(DBConstants.TABLE_QUESTION,
					DBConstants.EVENT_ID, new String[]{String.valueOf(id)});
			if(result != 0){
				delCount++;
			}
		}
		//设置事务标志为成功，当结束事务时就会提交事务
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
		return delCount;
	}*/

	/*public synchronized int refresh(ArrayList<QuestionInfo> infos) {
		// TODO Auto-generated method stub
		clear();
		int count = save(infos);
		return count;
	}*/
}
